package com.business.user.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.business.common.dto.R;
import com.business.common.enums.ErrorEnum;
import com.business.common.utils.StrKit;
import com.business.redis.ExpiresTimeEnum;
import com.business.redis.constant.RedisKeyConstant;
import com.business.redis.utils.RedisUtil;
import com.business.user.annotation.TokenPermission;
import com.business.user.entity.UserDetails;
import com.business.user.entity.UserDto;
import com.business.user.service.UserDetailsService;
import com.business.user.service.UserTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 用户标签表: 后端controller类
 *
 * @author chenjiehong
 * @date 2019-11-29 14:28:27
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/tag")
public class UserTagController {


    @Autowired
    private UserTagService userTagServiceImpl;

    @Autowired
    private UserDetailsService userDetailsServiceImpl;

    private void removeRedisData(Integer userId) {
        String key2 = String.format(RedisKeyConstant.USER_DETAILS_QUERY_TAG_LIST, userId);
        RedisUtil.delValue(key2);
    }

    /**
     * 新增用户标签表
     *
     * @param params
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/add")
    @ResponseBody
    @TokenPermission
    public R add(@RequestBody JSONObject params, UserDto user) {
        String tag = params.getString("tag");
        if (StrKit.isBlank(tag)) {
            return R.error(ErrorEnum.PARAMS_LOSE, "tag");
        }
        Integer r = userTagServiceImpl.add(user.getId(), tag);
        removeRedisData(user.getId());
        return R.success(r);

    }


    /**
     * 新增用户标签表
     *
     * @param params
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/addList")
    @ResponseBody
    @TokenPermission
    public R addList(@RequestBody JSONObject params, UserDto user) {
        JSONArray array = params.getJSONArray("tagList");
        if (array == null) {
            return R.error(ErrorEnum.PARAMS_ERROR, "tagList 不存在");
        }
        List<String> list = array.toJavaList(String.class);
        Integer r = userTagServiceImpl.addList(user.getId(), list);
        removeRedisData(user.getId());
        return R.success(r);
    }


    /**
     * 查询用户标签表
     *
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/queryByUser")
    @ResponseBody
    @TokenPermission
    public R queryByUser(UserDto user) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", user.getId());
        return queryTagList(jsonObject);
    }

    /**
     * 查询用户标签list
     *
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/queryTagList")
    @ResponseBody
    public R queryTagList(@RequestBody JSONObject jsonObject) {
        Integer userId = jsonObject.getIntValue("userId");
        String key = String.format(RedisKeyConstant.USER_DETAILS_QUERY_TAG_LIST, userId);
        if (RedisUtil.hasKey(key)) {
            Object list = RedisUtil.getValue(key);
            return R.success(list);
        } else {
            String[] r = userTagServiceImpl.queryTagList(userId);
            RedisUtil.setValue(key, r, ExpiresTimeEnum.LEVEL_8.getExpiresTime());
            return R.success(r);
        }

    }


    /**
     * 批量删除用户标签表
     *
     * @param params
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/deleteList")
    @ResponseBody
    @TokenPermission
    public R deleteList(@RequestBody JSONObject params, UserDto user) {
        JSONArray array = params.getJSONArray("tagList");
        if (array == null) {
            return R.error(ErrorEnum.PARAMS_ERROR, "tagList 不存在");
        }
        List<String> idList = array.toJavaList(String.class);
        Integer r = userTagServiceImpl.deleteList(user.getId(), idList);
        removeRedisData(user.getId());
        return R.success(r);
    }


    /**
     * 删除用户标签表
     *
     * @param params
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/delete")
    @ResponseBody
    @TokenPermission
    public R delete(@RequestBody JSONObject params, UserDto user) {
        String tag = params.getString("tag");
        Integer r = userTagServiceImpl.delete(user.getId(), tag);
        removeRedisData(user.getId());
        return R.success(r);
    }

    /**
     * 查询热门标签list
     *
     * @date 2020-02-22
     */
    @PostMapping("/queryHotTagList")
    @ResponseBody
    @TokenPermission
    public R queryHotTagList(UserDto user) {
        String key = String.format(RedisKeyConstant.USER_DETAILS_QUERY_USER,user.getId());
        UserDetails userDetails = null;
        if (RedisUtil.hasKey(key)) {
            userDetails = (UserDetails) RedisUtil.getObject(key, UserDetails.class);
        } else {
            userDetails = userDetailsServiceImpl.query(user.getId());
            RedisUtil.setValue(key, userDetails, ExpiresTimeEnum.LOGIN.getExpiresTime());
        }
        String key2 = String.format(RedisKeyConstant.USER_DETAILS_HOT_TAG_LIST, userDetails.getParentCategoryCode(), userDetails.getSubCategoryCode());
        Set<Object> tags = RedisUtil.getZSetValues(key2, 0, -1);
        if (tags == null) {//如果子行业没热门标签查夫类的
            String key3 = String.format(RedisKeyConstant.USER_DETAILS_HOT_TAG_LIST, userDetails.getParentCategoryCode());
            tags = RedisUtil.getZSetValues(key3, 0, -1);
        }
        return R.success(tags);
    }


}